/*
 * RoboEye - Web client for www.newocr.com that allows you
 * to recognize an image captured by the camera.
 *
 * Copyright (C) Oxygen Development, 2011
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.devoxy.roboeye.global;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import com.devoxy.roboeye.core.provider.Task;
import com.devoxy.roboeye.ui.activity.AboutActivity;
import com.devoxy.roboeye.ui.activity.FileBrowserActivity;
import com.devoxy.roboeye.ui.activity.PreferencesActivity;
import com.devoxy.roboeye.ui.activity.SplashActivity;
import com.devoxy.roboeye.ui.activity.task_list.TaskListActivity;

import java.io.File;
import java.util.List;

/**
 * Created by Oxygen Development.
 * Author: Dmitriy Tarasov (dm.vl.tarasov@gmail.com)
 * Date: 04.05.11
 * Time: 1:18
 */
public class IntentHelper {

    // there will be singletons
    public static final String ACTION_ADD_TASK = "com.devoxy.roboeye.action.ADD_TASK";
    public static final String ACTION_CANCEL_TASK = "com.devoxy.roboeye.action.CANCEL_TASK";
    public static final String ACTION_CLEAR_FINISHED = "com.devoxy.roboeye.action.CLEAR_FINISHED";
    public static final String ACTION_CANCEL_ALL = "com.devoxy.roboeye.action.CANCEL_ALL";

    public static final String PATH = "path";

    public static final String INIT_LOCATION = "init_location";

    private static final String MIME_TYPE_PLAIN_TEXT = "text/plain";
    private static final String MIME_TYPE_IMAGE = "image/*";

    private static final Context context;

    static {
        context = ContextHolder.getInstance().getContext();
    }

    public static Intent getAboutIntent() {
        return new Intent(context, AboutActivity.class);
    }

    public static Intent getCaptureIntent(String pathToFile) {
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(pathToFile)));
        return i;
    }

    public static Intent getPreferencesIntent() {
        return new Intent(context, PreferencesActivity.class);
    }

    public static Intent getFileBrowserIntent(CharSequence initLocation) {
        Intent i = new Intent(context, FileBrowserActivity.class);
        if (!TextUtils.isEmpty(initLocation)) {
            i.putExtra(INIT_LOCATION, initLocation);
        }
        return i;
    }

    public static Intent getTaskListIntent() {
        return new Intent(context, TaskListActivity.class);
    }

    public static Intent getAddTaskIntent(boolean isDirectory, String source, String target, String language, String rotation) {
        Intent i = new Intent(ACTION_ADD_TASK);
        i.putExtra(Task.TYPE, isDirectory);
        i.putExtra(Task.SOURCE, source);
        i.putExtra(Task.TARGET, target);
        i.putExtra(Task.LANGUAGE, language);
        i.putExtra(Task.ROTATION, rotation);
        return i;
    }

    public static Intent getCancelTaskIntent(long taskId) {
        Intent i = new Intent(ACTION_CANCEL_TASK);
        i.putExtra(Task._ID, taskId);
        return i;
    }

    public static Intent getClearFinishedTasksIntent() {
        return new Intent(ACTION_CLEAR_FINISHED);
    }

    public static Intent getCancelAllIntent() {
        return new Intent(ACTION_CANCEL_ALL);
    }

    public static Intent getOpenTextIntent(String path) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.addCategory(Intent.CATEGORY_DEFAULT);
        // TODO encode string - replace " " by %20
        Uri data = Uri.parse(String.format("file://%s", path));
        i.setDataAndType(data, MIME_TYPE_PLAIN_TEXT);
        return i;
    }

    public static Intent getOpenImageIntent(String path) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.addCategory(Intent.CATEGORY_DEFAULT);
        // TODO encode string - replace " " by %20
        Uri data = Uri.parse(String.format("file://%s", path));
        i.setDataAndType(data, MIME_TYPE_IMAGE);
        return i;
    }

    public static Intent getSplashIntent() {
        return new Intent(context, SplashActivity.class);
    }

    public static boolean isIntentAvailable(Intent intent) {
        final PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }

}
